﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Gauges;
using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.Platform.Core.ServiceModel;
using Baosight.Platform.Core;
using Baosight.ZNH.ZMCommon;


namespace Baosight.ZNH.ZMCA.UI 
{
    public partial class ZMCA00 : EFViewCmes
    {
        ImageSource imgGetMoreSource = Common.GetImageSource("/Images/GetMore.jpg");
        ImageSource imgGetMoreUnSelectedSource = Common.GetImageSource("/Images/GetMoreUnSelected.jpg");
        ImageSource imgFoldGetMoreSource = Common.GetImageSource("/Images/FoldGetMore.jpg");
        ImageSource imgFoldGetMoreUnSelectedSource = Common.GetImageSource("/Images/FoldGetMoreUnSelected.jpg");

        public ZMCA00()
        {
            InitializeComponent();

            //先将图片soure设置成选中再设置成未选中，是为了将图片加载进来，否则系统会延迟加载，即会在用户鼠标移到操作图片上之后再加载选中状态的图片，界面会有跳跃感。
            imgGetMore.Source = imgGetMoreSource;
            imgFoldGetMore.Source = imgFoldGetMoreSource;
            this.Loaded += new RoutedEventHandler(ZMCA00_Loaded);

        }

        void ZMCA00_Loaded(object sender, RoutedEventArgs e)
        {
            imgGetMore.Source = imgGetMoreUnSelectedSource;
            imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }

        public override void Initialize(EIInfo initData)
        {
            base.Initialize(initData);
            ZMCA03Controler.InitWarningPassRate(initData);

            LoadGaugeData(initData);
            RegEventForGauges();
            LoadIssueData();
        }


        /// <summary>
        /// 对于用户指定的指标，前四个显示在默认的LayoutBasicIndex表格中，其他的显示在LayoutMoreIndex表格中
        /// 每行显示2个
        /// </summary>
        /// <param name="initData"></param>
        private void LoadGaugeData(EIInfo initData)
        {
            EIBlock indexBlock = initData.GetBlock("index");
            for (int i = 0; i < indexBlock.Rows.Count && i<6; i++)
            {
                EIRow index = indexBlock.Rows[i];
                string indexName = index["fdDetailsName"].ToString();
                UserControl c = GetGaugeByName(indexName);
                if (c != null)
                {
                    int column = i % 3;
                    int row = i / 3;
                    c.SetValue(Grid.ColumnProperty, column);
                    c.SetValue(Grid.RowProperty, row);
                    LayoutBasicIndex.Children.Add(c);
                }
            }

            if (indexBlock.Rows.Count > 6)
            {
                //假设用户选了7个指标，则LayoutMoreIndex里面应该包括2行：(7-4+1)/2 = 2;
                //假设用户选了6个指标，则LayoutMoreIndex里面应该包括1行：(6-4+1)/2 = 1;
                int rowNumber = (indexBlock.Rows.Count - 6 +1) / 2;
                for (int i = 0; i < rowNumber; i++)
                {
                    RowDefinition cd = new RowDefinition();
                    cd.Height = new GridLength(188, GridUnitType.Pixel);
                    LayoutMoreIndex.RowDefinitions.Add(cd);
                }
                for (int i = 6; i < indexBlock.Rows.Count; i++)
                {
                    EIRow index = indexBlock.Rows[i];
                    string indexName = index["fdDetailsName"].ToString();
                    UserControl c = GetGaugeByName(indexName);
                    if (c != null)
                    {
                        int column = (i - 6) % 2;
                        int row = (i - 6) / 2;
                        c.SetValue(Grid.ColumnProperty, column);
                        c.SetValue(Grid.RowProperty, row);
                        LayoutMoreIndex.Children.Add(c);
                    }
                }

            }
        }

        //回采工作面效率
        //原煤全员效率
        //原煤产量
        //综合进尺
        //开拓进尺
        //安全隐患
        //井下三违
        //材料消耗
        private UserControl GetGaugeByName(string indexName)
        {
            UserControl c = null;
            switch (indexName)
            {
                case "回采工作面效率":
                    {
            /*            ZMCA01Controler controler = new ZMCA01Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.Tag = "ZMCA01";
                        controler.GetGaugeData(gauge);                    
                        c = gauge;*/

                        ZMCA06Controler controler = new ZMCA06Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.LeftTag = "ZMCA02";
                        gauge.RightTag = "ZMCA01";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "原煤全员效率":
                    {
            /*            ZMCA02Controler controler = new ZMCA02Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.Tag = "ZMCA02";
                        controler.GetGaugeData(gauge);
                        c = gauge;*/
                        ZMCA15Controler controler = new ZMCA15Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.LeftTag = "ZMCA12";
                        gauge.RightTag = "ZMCA13";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                      
                        break;
                    }
                case "原煤产量":
                    {
                        ZMCA03Controler controler = new ZMCA03Controler();
                        TriCircleGauge gauge = new TriCircleGauge();
                        gauge.Tag = "ZMCA03";
                        controler.GetGaugeData(gauge);
                        //gauge.leftNeedle.Value = 87;
                        //gauge.centerNeedle.Value = 57;
                        //gauge.rightNeedle.Value = 99;
                        //gauge.leftControler.SetMarkerValer(60, 90);
                        //gauge.centerControler.SetMarkerValer(60, 90);
                        //gauge.rightControler.SetMarkerValer(60, 90);

                        //gauge.gaugeTitle.Text = "原煤产量";
                        //gauge.leftGaugeDescription.Text = "万吨(日)";
                        //gauge.centerGaugeDescription.Text = "十万吨(年)";
                        //gauge.rightGaugeDescription.Text = "百万吨(月)";

                        c = gauge;
                        break;
                    }
                case "综合进尺":
                    {
                        ZMCA03Controler controler = new ZMCA03Controler();
                        controler.PlanType = "掘进计划";
                        TriCircleGauge gauge = new TriCircleGauge();
                        gauge.Tag = "ZMCA04";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "开拓进尺":
                    {
                        ZMCA03Controler controler = new ZMCA03Controler();
                        controler.PlanType = "掘进计划";
                        controler.IsDeploitation = "True";
                        TriCircleGauge gauge = new TriCircleGauge();
                        gauge.Tag = "ZMCA05";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "安全隐患":
                    {
                        ZMCA12Controler controler = new ZMCA12Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.Tag = "ZMCA12";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "井下三违":
                    {
                        ZMCA13Controler controler = new ZMCA13Controler();
                        DoubleCircleGauge gauge = new DoubleCircleGauge();
                        gauge.Tag = "ZMCA13";
                        controler.GetGaugeData(gauge);
                        c = gauge;
                        break;
                    }
                case "材料消耗":
                    {
                        ZMCA14Controler controler = new ZMCA14Controler();
                        SingleCircleGauge gauge = new SingleCircleGauge();
                        gauge.Tag = "ZMCA14";
                        controler.GetGaugeData(gauge);

                        //gauge.centerNeedle.Value = 57;
                        //gauge.centerControler.SetMarkerValer(60, 90);


                        //gauge.gaugeTitle.Text = "材料消耗";
                        //gauge.centerGaugeDescription.Text = "百万元";

                        c = gauge;
                        break;
                    }
                default:
                    break;
            }
            c.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            return c;
        }

        private void LoadIssueData()
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            this.containerProblem.LoadView<EFCustomViewContainerPanel>("ZMCA44", args);
            Dictionary<string, string> arg = new Dictionary<string, string>();
            this.containerIndicate.LoadView<EFCustomViewContainerPanel>("ZMCA43", arg);
        }

        /// <summary>
        /// 由子页面刷新数据
        /// </summary>
        /// <param name="data"></param>
        public override void OnEIDataPublished(EIInfo data)
        {
            Dictionary<string, string> arg = new Dictionary<string, string>();
            this.containerIndicate.LoadView<EFCustomViewContainerPanel>("ZMCA43", arg);
        }
        #region mouse event
        private void RegEventForGauges()
        {
            foreach (var item in LayoutBasicIndex.Children)
            {
                RegMouseEvent(item as UserControl);
            }
            foreach (var item in LayoutMoreIndex.Children)
            {
                RegMouseEvent(item as UserControl);
            }
        }

        private void RegMouseEvent(UserControl c)
        {
            if (c is SingleCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as SingleCircleGauge).centerGauge);
            }
            else if (c is DoubleCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as DoubleCircleGauge).leftGauge);
                RegMouseEventForRadiaGauge((c as DoubleCircleGauge).rightGauge);
            }
            else if (c is TriCircleGauge)
            {
                RegMouseEventForRadiaGauge((c as TriCircleGauge).leftGauge);
                RegMouseEventForRadiaGauge((c as TriCircleGauge).centerGauge);
                RegMouseEventForRadiaGauge((c as TriCircleGauge).rightGauge);
            }
        }

        private void RegMouseEventForRadiaGauge(RadialGauge rg)
        {
            rg.MouseEnter += new MouseEventHandler(gauge_MouseEnter);
            rg.MouseLeave += new MouseEventHandler(gauge_MouseLeave);
            rg.MouseLeftButtonDown += new MouseButtonEventHandler(leftGauge_MouseLeftButtonDown);
        }

        void gauge_MouseLeave(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        void gauge_MouseEnter(object sender, MouseEventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        void leftGauge_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            object tag = null;
            if (sender is RadialGauge)
            {
                if((sender as RadialGauge).Name == "leftGauge")
                     tag = (((sender as RadialGauge).Parent as Canvas).Parent as DoubleCircleGauge).LeftTag;
                else if((sender as RadialGauge).Name == "rightGauge")
                     tag = (((sender as RadialGauge).Parent as Canvas).Parent as DoubleCircleGauge).RightTag;
             
                if (tag != null)
                {
                    Common.LoadView(this, tag.ToString());
                }
            }
        }

        private void imgGetMore_MouseEnter(object sender, MouseEventArgs e)
        {
            imgGetMore.Source = imgGetMoreSource;
        }

        private void imgGetMore_MouseLeave(object sender, MouseEventArgs e)
        {
            imgGetMore.Source = imgGetMoreUnSelectedSource;
        }

        private void imgGetMore_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutMoreIndex.Visibility = System.Windows.Visibility.Visible;
            imgFoldGetMore.Visibility = System.Windows.Visibility.Visible;
            imgGetMore.Visibility = System.Windows.Visibility.Collapsed;
            spnlGrid.Visibility = System.Windows.Visibility.Collapsed;
            imgGetMore.Source = imgGetMoreUnSelectedSource;
        }

        private void imgFoldGetMore_MouseEnter(object sender, MouseEventArgs e)
        {
            imgFoldGetMore.Source = imgFoldGetMoreSource;
        }

        private void imgFoldGetMore_MouseLeave(object sender, MouseEventArgs e)
        {
            imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }

        private void imgFoldGetMore_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            LayoutMoreIndex.Visibility = System.Windows.Visibility.Collapsed;
            imgFoldGetMore.Visibility = System.Windows.Visibility.Collapsed;
            imgGetMore.Visibility = System.Windows.Visibility.Visible;
            spnlGrid.Visibility = System.Windows.Visibility.Visible;
            imgFoldGetMore.Source = imgFoldGetMoreUnSelectedSource;
        }
        #endregion
    }
}
